{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1617241a-a1af-43f5-b8a0-1e70f0a44636",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xarray as xr\n",
    "import matplotlib.pyplot as plt\n",
    "import cartopy.crs as ccrs\n",
    "import cartopy.feature as cfeature\n",
    "from copy import copy\n",
    "from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER\n",
    "import shapely.geometry as sgeom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bba338dd-6d07-4200-b3cf-a02475bb25c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.Dataset>\n",
      "Dimensions:    (longitude: 3600, latitude: 1801, time: 1)\n",
      "Coordinates:\n",
      "  * longitude  (longitude) float32 0.0 0.1 0.2 0.3 ... 359.6 359.7 359.8 359.9\n",
      "  * latitude   (latitude) float32 90.0 89.9 89.8 89.7 ... -89.8 -89.9 -90.0\n",
      "  * time       (time) datetime64[ns] 2018-02-01\n",
      "Data variables:\n",
      "    t2m        (time, latitude, longitude) float32 ...\n",
      "Attributes:\n",
      "    Conventions:  CF-1.6\n",
      "    history:      2024-01-24 18:13:56 GMT by grib_to_netcdf-2.24.0: /opt/ecmw...\n"
     ]
    }
   ],
   "source": [
    "#加载NC资料，用相对路径可能会报错，如果错了可以试试绝对路径\n",
    "#ds指Dataset，另外还有da指DataArray，是xarray的一种数据结构\n",
    "ds = xr.open_dataset('/storage/data/raster/netcdf/EC-Interim_monthly_2018.nc')\n",
    "print(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "234979e2-c7af-4c0d-bff0-e0d2f5127d67",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.DataArray 't2m' (time: 1, latitude: 1801, longitude: 3600)>\n",
      "array([[[       nan,        nan,        nan, ...,        nan,\n",
      "                nan,        nan],\n",
      "        [       nan,        nan,        nan, ...,        nan,\n",
      "                nan,        nan],\n",
      "        [       nan,        nan,        nan, ...,        nan,\n",
      "                nan,        nan],\n",
      "        ...,\n",
      "        [-37.184387, -37.184387, -37.184387, ..., -37.18686 ,\n",
      "         -37.184387, -37.184387],\n",
      "        [-37.19429 , -37.19429 , -37.19429 , ..., -37.196762,\n",
      "         -37.19429 , -37.19429 ],\n",
      "        [-38.0793  , -38.0793  , -38.0793  , ..., -38.0793  ,\n",
      "         -38.0793  , -38.0793  ]]], dtype=float32)\n",
      "Coordinates:\n",
      "  * longitude  (longitude) float32 0.0 0.1 0.2 0.3 ... 359.6 359.7 359.8 359.9\n",
      "  * latitude   (latitude) float32 90.0 89.9 89.8 89.7 ... -89.8 -89.9 -90.0\n",
      "  * time       (time) datetime64[ns] 2018-02-01\n"
     ]
    }
   ],
   "source": [
    "temp = (ds['t2m'] - 273.15) # 把温度转换为℃\n",
    "print(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f01c1ea5-b504-4e6c-91b8-8127b4893069",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39",
   "language": "python",
   "name": "py39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
